﻿using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.Common;
using TianShiProject.Models.HumanAffairs.Entrys;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.HumanAffairs.Entrys
{
    public class EntryRepository : DapperDbContextRepository, IEntryRepository
    {
        public EntryRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        #region 入职办理——查询分页显示
        /// <summary>
        /// 入职办理——查询分页显示
        /// </summary>
        /// <param name="EntryName"></param>
        /// <param name="DeptId"></param>
        /// <param name="PositionId"></param>
        /// <param name="EntryTime"></param>
        /// <param name="State"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<EntryDto>> GetEntryPageShow(string EntryName, int DeptId, int PositionId, DateTime? EntryTime, int State, int pageindex = 1, int pagesize = 3)
        {
            PageResponse<EntryDto> response = new PageResponse<EntryDto>();
            var sql = "select a.*,b.DeptName as BName,c.PositionName as CName,d.DictionaryName as DName from Entry a join Department b on a.DeptId=b.Id join Position c on a.PositionId=c.Id join Dictionary d on a.State=d.Id where a.IsDelete=0";
            if (!string.IsNullOrWhiteSpace(EntryName))
            {
                sql += $" and a.EntryName like '%" + EntryName + "%'";
            }
            if (DeptId > 0)
            {
                sql += $" and a.DeptId='{DeptId}'";
            }
            if (PositionId > 0)
            {
                sql += $" and a.PositionId='{PositionId}'";
            }
            if (EntryTime!=null)
            {
                sql += $" and a.EntryTime='{EntryTime}'";
            }
            if (State > 0)
            {
                sql += $" and a.State='{State}'";
            }
            var list = await this.QueryAsync<EntryDto>(sql, null);
            response.PageTotal = list.Count;
            sql += $" order by a.Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<EntryDto>(sql, null);
            response.PageData = lists;
            return response;
        }
        #endregion

        #region 入职办理——逻辑删除
        /// <summary>
        /// 入职办理——逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteEntry(int id)
        {

            var sql = $"update Entry set IsDelete=1 where Id={id}";
            return await ExecuteAsync(sql, null);
        }
        #endregion

        #region 入职办理——批量删除
        /// <summary>
        /// 入职办理——批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DeleteBatchEntry(string ids)
        {
            var list = 0;
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update Entry set IsDelete=1 where Id={item}";
                list = await ExecuteAsync(sql, null);
            }
            return list;
        }
        #endregion

        #region 入职办理——编辑
        /// <summary>
        /// 入职办理——编辑
        /// </summary>
        /// <param name="entryDto"></param>
        /// <returns></returns>
        public async Task<int> PutUpdateEntry(EntryDto entryDto)
        {
            var sql = $"update Base set EntryNumber='{entryDto.EntryNumber}',EntryName='{entryDto.EntryName}',DeptId='{entryDto.DeptId}',PositionId='{entryDto.PositionId}',SuperiorManager='{entryDto.SuperiorManager}',EntryTime='{entryDto.EntryTime}',CreateTime='{entryDto.CreateTime}',State='{entryDto.State}',Remark='{entryDto.Remark}',IsDelete='{entryDto.IsDelete}' where Id='{entryDto.Id}'";
            return await ExecuteAsync(sql, entryDto);
        }
        #endregion

        #region 入职办理——添加
        /// <summary>
        /// 入职办理——添加
        /// </summary>
        /// <param name="entryDto"></param>
        /// <returns></returns>
        public async Task<int> PostAddEntry(EntryDto entryDto)
        {
            var worker = new IdWorker(1, 1);
            var id = worker.NextId().ToString();
            var sql = $"insert into RecipientsToAppl values('{id}','{entryDto.EntryName}','{entryDto.DeptId}','{entryDto.PositionId}','{entryDto.SuperiorManager}',getdate(),getdate(),'{entryDto.State}','{entryDto.Remark}',0)";
            return await ExecuteAsync(sql, entryDto);
        }
        #endregion
    }
}
